package cn.edu.sdjzu.xg.eagle.service;

import cn.edu.sdjzu.xg.eagle.dao.SchoolDaoImpl;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.factory.DaoFactory;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class SchoolServiceImpl implements SchoolServiceInterface {
    //private static final SchoolDaoInterface schoolDao = SchoolDaoImpl.getInstance();
    //改为公有创造
    SchoolDaoImpl schoolDao = DaoFactory.getDao(SchoolDaoImpl.class,"SchoolDaoImpl");
    private static final SchoolServiceImpl instance = new SchoolServiceImpl();

    public SchoolServiceImpl() {}

    public static SchoolServiceImpl getInstance() {
        return instance;
    }

    @Override
    public School find(Long id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        try {
            return schoolDao.find(id);
        } finally {
            JdbcHelper.close(connection);
        }
    }

    @Override
    public Collection<School> findAll() throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        try {
            return schoolDao.findAll();
        } finally {
            JdbcHelper.close(connection);
        }
    }

    @Override
    public Collection<School> findAll(Pagination pagination) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        try {
            return schoolDao.findAll();
        } finally {
            JdbcHelper.close(connection);
        }
    }

    @Override
    public boolean add(School school) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        try {
            return schoolDao.add(connection, school);
        } finally {
            JdbcHelper.close(connection);
        }
    }

    @Override
    public boolean update(School school) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        try {
            return schoolDao.update(connection, school);
        } finally {
            JdbcHelper.close(connection);
        }
    }

    @Override
    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        try {
            return schoolDao.delete(id);
        } finally {
            JdbcHelper.close(connection);
        }
    }
}